package com.jiantong.admin.controller.management;

import com.google.common.base.Strings;
import com.jiantong.admin.dto.CityAreaDto;
import com.jiantong.admin.dto.UserAllDto;
import com.jiantong.admin.model.Common;
import com.jiantong.admin.model.PagingMz;
import com.jiantong.common.util.MD5Util;

import com.jiantong.user.model.*;
import com.jiantong.user.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by ThinkPad on 2017/8/2.
 */
@Controller
@RequestMapping("/management/user")
public class UserManagementController {
    @RpcConsumer
    private UserReadService userReadService;
    @RpcConsumer
    private UserProfileReadService userProfileReadService;
    @RpcConsumer
    private OrganizationUserReadService organizationUserReadService;
    @RpcConsumer
    private DepartmentUserReadService departmentUserReadService;
    @RpcConsumer
    private UserWriteService userWriteService;
    @RpcConsumer
    private UserProfileWriteService userProfileWriteService;
    @RpcConsumer
    private OrganizationUserWriteService organizationUserWriteService;
    @RpcConsumer
    private DepartmentUserWriteService departmentUserWriteService;
    @RpcConsumer
    private AreaReadService areaReadService;
    @RpcConsumer
    private OrganizationReadService organizationReadService;
    @RpcConsumer
    private DepartmentReadService departmentReadService;
    @RpcConsumer
    private UserRoleReadService userRoleReadService;
    @RpcConsumer
    private UserRoleWriteService userRoleWriteService;
    @RpcConsumer
    private RolesWebReadService rolesWebReadService;
    @RpcConsumer
    private RolesWebWriteService rolesWebWriteService;
    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;
    @RpcConsumer
    private GovernmentUserDutyReadService governmentUserDutyReadService;
    @RpcConsumer
    private ButcherFactoryReadService butcherFactoryReadService;
    @RpcConsumer
    private AppUserRoleWriteService appUserRoleWriteService;

    @GetMapping("/show")
    public String showuser(Model model, HttpServletRequest request, HttpServletResponse response) {

        List<String> organ = organizationUserReadService.selectorgan().getResult();
        Map map = getarea(response, request);
        Common common = new Common(breedFactoryReadService, butcherFactoryReadService);
        // List<CityAreaDto> areaDtoList = common.serachCityAreaDtoInBreefctory();
        model.addAttribute("userProfile", map.get("userProfile"));

        model.addAttribute("cityAreaDtoList", map.get("cityAreaDtoList"));
        model.addAttribute("organs", organ);
        model.addAttribute("username", map.get("username"));
        model.addAttribute("OrganizationType", map.get("OrganizationType"));

        return "management/userManagement/usermanagementList";
    }

    @GetMapping("/userList")
    @ResponseBody
    public PagingMz<UserAllDto> userList(@RequestParam(required = false) Integer page,
                                         @RequestParam(required = false) Integer rows,
                                         @RequestParam(required = false) Integer usertype,//OrganizationUser  organizationType
                                         @RequestParam(required = false) String region,//UserProfile region
                                         @RequestParam(required = false) String company,//OrganizationUser organizationName
                                         @RequestParam(required = false) String department,//DepartmentUser departmentName
                                         @RequestParam(required = false) String name,//UserProfile realName
                                         @RequestParam(required = false) String mobile//User mobile
    ) {
        Map criteria = new HashMap();
        criteria.put("status", 1);
        if (!Strings.isNullOrEmpty(mobile)) {
            criteria.put("mobile", mobile);
        }

        List<User> userList = (List<User>) userReadService.selectAlUser(criteria).getResult();
        Integer usize = 0;
        List<UserAllDto> resultList = new ArrayList<>();
        criteria.clear();
        if (usertype != null && usertype != 0) {
            criteria.put("organizationType", usertype);
        }
        if (!Strings.isNullOrEmpty(company)) {
            criteria.put("organizationName", company);
        }
        List<OrganizationUser> ouserList = organizationUserReadService.selectList(criteria).getResult();
        Integer osize = 0;
        criteria.clear();
        if (!Strings.isNullOrEmpty(region)) {
            criteria.put("region", region);
        }
        if (!Strings.isNullOrEmpty(name)) {
            criteria.put("realName", name);
        }
        List<UserProfile> puserList = userProfileReadService.selectList(criteria).getResult();
        Integer psize = 0;
        criteria.clear();
        if (!Strings.isNullOrEmpty(department)) {
            criteria.put("departmentName", department);
        }
        List<DepartmentUser> duserList = departmentUserReadService.selectList(criteria).getResult();
        Integer dsize = 0;

        if (userList != null) {
            usize = userList.size();
        }

        if (ouserList != null) {
            osize = ouserList.size();
        }

        if (puserList != null) {
            psize = puserList.size();
        }

        if (duserList != null) {
            dsize = duserList.size();
        }
        List<Long> ids = new ArrayList();
        List<Long> oids = new ArrayList();
        List<Long> pids = new ArrayList();
        List<Long> dids = new ArrayList();

        for (User user : userList) {
            ids.add(user.getId());
        }
        for (UserProfile user : puserList) {
            pids.add(user.getUserId());
        }
        ids.retainAll(pids);
        for (OrganizationUser user : ouserList) {
            oids.add(user.getUserId());
        }
        ids.retainAll(oids);
        if (usertype == 3) {
            for (DepartmentUser user : duserList) {
                dids.add(user.getUserId());
            }
            ids.retainAll(dids);
        }

        for (Long id : ids) {
            UserAllDto userAllDto = new UserAllDto();
            Iterator iterator = userList.iterator();
            Iterator iteratoro = ouserList.iterator();
            Iterator iteratorp = puserList.iterator();
            Iterator iteratord = duserList.iterator();
            while (iterator.hasNext()) {
                User user = (User) iterator.next();
                if (user.getId() == id) {
                    userAllDto.setUser(user);
                    iterator.remove();
                    break;
                }
            }
            while (iteratoro.hasNext()) {
                OrganizationUser user = (OrganizationUser) iteratoro.next();
                if (id.equals(user.getUserId())) {
                    userAllDto.setOrganizationUser(user);
                    iteratoro.remove();
                    break;
                }
            }
            while (iteratorp.hasNext()) {
                UserProfile user = (UserProfile) iteratorp.next();
                if (id.equals(user.getUserId())) {
                    userAllDto.setUserProfile(user);
                    iteratorp.remove();
                    break;
                }
            }
            while (iteratord.hasNext()) {
                DepartmentUser user = (DepartmentUser) iteratord.next();
                if (id.equals(user.getUserId())) {
                    userAllDto.setDepartmentUser(user);
                    iteratord.remove();
                    break;
                }
            }
            resultList.add(userAllDto);
        }
        List<UserAllDto> list = new ArrayList<>();
        if (resultList.size() > rows) {
            list = resultList.subList(rows * (page - 1), (rows * page) > (resultList.size()) ? resultList.size() : rows * page);
        } else {
            list = resultList;
        }
        Integer i = resultList.size();
        Long records = Long.parseLong(i.toString());
        Long pageTotal = (records - 1) / rows + 1;
        return new PagingMz<>(pageTotal, records, page, list);
    }

    @GetMapping("/showDetail")
    public String showDetail() {

        return "management/userManagement/userDetail";

    }

    @GetMapping("/adduserGov")
    public String addUserGov(Model model, HttpServletResponse response, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        User user = new User();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id = cookie.getValue();
                int Id = Integer.parseInt(id);
                user = (User) userReadService.findById(Integer.toUnsignedLong(Id)).getResult();

            }
        }
        Map criteria = new HashMap();
        criteria.put("level", 1);
        List<Area> areaList = areaReadService.selectsubordinate(criteria).getResult();
        Map map = new HashMap();
        map.put("type", 3);
        List<RolesWeb> roles = (List<RolesWeb>) rolesWebReadService.findbykeyword(map).getResult();
        criteria.clear();
        criteria.put("status", 1);
        criteria.put("type", 3);
        List<Organization> organList = organizationReadService.selectList(criteria).getResult();
        model.addAttribute("level", user.getAreaLevel());
        model.addAttribute("organList", organList);
        model.addAttribute("areaList", areaList);
        model.addAttribute("roles", roles);
        return "management/userManagement/userGovAdd";
    }

    @GetMapping("/adduserFac")
    public String addUserfac(Model model) {
        Map criteria = new HashMap();
        criteria.put("level", 1);
        List<Area> areaList = areaReadService.selectsubordinate(criteria).getResult();
        List<RolesWeb> roles = (List<RolesWeb>) rolesWebReadService.findbykeyword(new HashMap()).getResult();
        criteria.clear();
        criteria.put("status", 1);
        criteria.put("type", 1);
        List<Organization> organList = organizationReadService.selectList(criteria).getResult();
        model.addAttribute("organList", organList);
        model.addAttribute("areaList", areaList);
        model.addAttribute("roles", roles);
        return "management/userManagement/userFacAdd";
    }

    @ResponseBody
    @RequestMapping("/doAdd")
    public Boolean doAddUser(@RequestParam String name,
                             @RequestParam String realname,
                             @RequestParam Integer gender,
                             @RequestParam Integer usertype,
                             @RequestParam Integer level,
                             @RequestParam(required = false) Long roleId,
                             @RequestParam(required = false) String roleName,
                             @RequestParam Integer provinceId,
                             @RequestParam Integer cityId,
                             @RequestParam Integer regionId,
                             @RequestParam Integer streetId,
                             @RequestParam(required = false) Integer villageId,
                             @RequestParam Long organizationId,
                             @RequestParam(required = false) Long company,
                             @RequestParam String mobile,
                             @RequestParam String email,
                             @RequestParam String password
    ) {

        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setMobile(mobile);
        user.setPassword(MD5Util.encrypt(password));
        user.setStatus(1);
        user.setAreaLevel(level);
        user.setType(2);
        UserProfile userProfile = new UserProfile();
        Map criteria = new HashMap();
        criteria.put("provinceId", provinceId);
        criteria.put("cityId", cityId);
        criteria.put("regionId", regionId);
        criteria.put("streetId", streetId);
        if (villageId == null) {
            criteria.put("villageId", villageId);
        }

        Map map = areaReadService.selectids(criteria).getResult();

        userProfile.setGender(gender);
        userProfile.setRealName(realname);
        userProfile.setProvinceId(provinceId);
        userProfile.setProvince((String) map.get("province"));
        userProfile.setCityId(cityId);
        userProfile.setCity((String) map.get("city"));
        userProfile.setRegionId(regionId);
        userProfile.setRegion((String) map.get("region"));
        userProfile.setStreetId(streetId);
        userProfile.setStreet((String) map.get("street"));
        if (villageId != null) {
            userProfile.setVillage((String) map.get("village"));
            userProfile.setVillageId(villageId);
        }


        OrganizationUser organizationUser = new OrganizationUser();
        Organization organization = organizationReadService.findById(organizationId).getResult();
        organizationUser.setOrganizationName(organization.getName());
        organizationUser.setOrganizationId(organizationId);
        organizationUser.setOrganizationType(organization.getType());
        organizationUser.setRoleName(roleName);
        criteria.clear();
        criteria.put("name", roleName);
        List<RolesWeb> rolesWebs = (List<RolesWeb>) rolesWebReadService.findbykeyword(criteria).getResult();
        RolesWeb rolesWeb = rolesWebs.get(0);
        if (rolesWebs == null && rolesWebs.isEmpty()) {
            organizationUser.setRoleType(0);
        } else {
            organizationUser.setRoleType(rolesWeb.getType());
        }
        Boolean b = false;
        if (usertype == 3) {
            Department department = departmentReadService.findDepartmentById(company).getResult();
            DepartmentUser departmentUser = new DepartmentUser();
            departmentUser.setUserName(realname);
            departmentUser.setDepartmentId(company);
            departmentUser.setDepartmentName(department.getName());
            departmentUser.setDepartmentType(department.getType());
            departmentUser.setStatus(1);
            departmentUser.setEmpType(1);
            AppUserRole appUserRole = new AppUserRole();
            appUserRole.setAppRole(roleId.intValue());
            appUserRole.setStatus(1);
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            b = (Boolean) userWriteService.createGov(user, userProfile, organizationUser, departmentUser, appUserRole, userRole).getResult();
        } else {
            b = (Boolean) userWriteService.createFac(user, userProfile, organizationUser).getResult();
        }

        return b;
    }

    @GetMapping("/updateuser")
    public String updateUserGov(@RequestParam Long id, Model model, HttpServletResponse response, HttpServletRequest request) {
        User user = (User) userReadService.findUserByUserId(id).getResult();
        OrganizationUser organizationUser = organizationUserReadService.findOrganizationUserByUserId(id).getResult();
        UserProfile userProfile = userProfileReadService.findUserProfileById(id).getResult();
        Map criteria = new HashMap();
        if (organizationUser.getOrganizationType() == 3) {
            DepartmentUser departmentUser = departmentUserReadService.findDepartmentUserByUserId(id).getResult();
            model.addAttribute("departmentUser", departmentUser);
            criteria.put("type", 3);
        } else {
            criteria.put("type", 1);
        }

        criteria.put("status", 1);

        List<Organization> organList = organizationReadService.selectList(criteria).getResult();
        model.addAttribute("organList", organList);
        model.addAttribute("user", user);
        model.addAttribute("organizationUser", organizationUser);
        model.addAttribute("userProfile", userProfile);
        criteria.clear();
        criteria.put("level", 1);
        List<Area> areaList = areaReadService.selectsubordinate(criteria).getResult();
        List<RolesWeb> roles = (List<RolesWeb>) rolesWebReadService.findbykeyword(new HashMap()).getResult();
        Cookie[] cookies = request.getCookies();
        User user1 = new User();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id1 = cookie.getValue();
                int Id = Integer.parseInt(id1);
                user1 = (User) userReadService.findById(Integer.toUnsignedLong(Id)).getResult();

            }
        }
        model.addAttribute("level", user1.getAreaLevel());
        model.addAttribute("areaList", areaList);
        model.addAttribute("roles", roles);
        return "management/userManagement/updateUser";
    }


    @PostMapping("/doupdate")
    @ResponseBody
    public Boolean doUpdateUser(@RequestParam Long id,
                                @RequestParam String name,
                                @RequestParam String realname,
                                @RequestParam Integer gender,
                                @RequestParam Integer usertype,
                                @RequestParam Integer level,
                                @RequestParam(required = false) String roleName,
                                @RequestParam Integer provinceId,
                                @RequestParam Integer cityId,
                                @RequestParam Integer regionId,
                                @RequestParam Integer streetId,
                                @RequestParam(required = false) Integer villageId,
                                @RequestParam Long organizationId,
                                @RequestParam(required = false) Long company,
                                @RequestParam String mobile,
                                @RequestParam String email
    ) {
        User user = (User) userReadService.findUserByUserId(id).getResult();
        user.setName(name);
        user.setEmail(email);
        user.setMobile(mobile);
        user.setStatus(1);
        user.setAreaLevel(level);
        user.setType(2);
        UserProfile userProfile = userProfileReadService.findUserProfileById(id).getResult();
        Map criteria = new HashMap();
        criteria.put("provinceId", provinceId);
        criteria.put("cityId", cityId);
        criteria.put("regionId", regionId);
        criteria.put("streetId", streetId);
        if (villageId != -1) {
            criteria.put("villageId", villageId);
        }
        Map map = areaReadService.selectids(criteria).getResult();

        userProfile.setGender(gender);
        userProfile.setRealName(realname);
        userProfile.setProvinceId(provinceId);
        userProfile.setProvince((String) map.get("province"));
        userProfile.setCityId(cityId);
        userProfile.setCity((String) map.get("city"));
        userProfile.setRegionId(regionId);
        userProfile.setRegion((String) map.get("region"));
        userProfile.setStreetId(streetId);
        if (villageId != -1) {
            userProfile.setVillage((String) map.get("village"));
            userProfile.setVillageId(villageId);
        }
        userProfile.setStreet((String) map.get("street"));

        OrganizationUser organizationUser = organizationUserReadService.findOrganizationUserByUserId(id).getResult();
        Organization organization = organizationReadService.findById(organizationId).getResult();
        organizationUser.setOrganizationName(organization.getName());
        organizationUser.setOrganizationId(organizationId);
        organizationUser.setOrganizationType(organization.getType());
        organizationUser.setRoleName(roleName);
        criteria.clear();
        criteria.put("name", roleName);
        List<RolesWeb> rolesWebs = (List<RolesWeb>) rolesWebReadService.findbykeyword(criteria).getResult();

        if (rolesWebs == null || rolesWebs.isEmpty()) {
            organizationUser.setRoleType(0);
        } else {
            RolesWeb rolesWeb = rolesWebs.get(0);
            organizationUser.setRoleType(rolesWeb.getType());
        }
        Boolean b = false;
        if (usertype == 3) {
            Department department = departmentReadService.findDepartmentById(company).getResult();
            DepartmentUser departmentUser = departmentUserReadService.findDepartmentUserByUserId(id).getResult();
            departmentUser.setUserName(realname);
            departmentUser.setDepartmentId(company);
            departmentUser.setDepartmentName(department.getName());
            departmentUser.setDepartmentType(department.getType());
            b = (Boolean) userWriteService.updateuser(user, userProfile, organizationUser, departmentUser).getResult();
        } else {
            DepartmentUser departmentUser = new DepartmentUser();
            b = (Boolean) userWriteService.updateuser(user, userProfile, organizationUser, departmentUser).getResult();
        }
        return b;
    }


    @PostMapping("/del")
    @ResponseBody
    public Boolean delUser(@RequestParam Long id) {
        User user = (User) userReadService.findById(id).getResult();
        user.setStatus(-2);
        Boolean b = (Boolean) userWriteService.update(user).getResult();
        Map criteria = new HashMap();
        criteria.put("userId", id);
        return b;

    }

    @GetMapping("/change")
    @ResponseBody
    public List<Area> subordinate(@RequestParam Integer level,
                                  @RequestParam Integer provinceId,
                                  @RequestParam(required = false) Integer cityId,
                                  @RequestParam(required = false) Integer regionId,
                                  @RequestParam(required = false) Integer streetId) {
        Map criteria = new HashMap();
        criteria.put("level", level);
        switch (level) {
            case 2:
                criteria.put("provinceId", provinceId);
                break;
            case 3:
                criteria.put("provinceId", provinceId);
                criteria.put("cityId", cityId);
                break;
            case 4:
                criteria.put("provinceId", provinceId);
                criteria.put("cityId", cityId);
                criteria.put("regionId", regionId);
                break;
            case 5:
                criteria.put("provinceId", provinceId);
                criteria.put("cityId", cityId);
                criteria.put("regionId", regionId);
                criteria.put("streetId", streetId);
                break;
        }
        List<Area> areaList = areaReadService.selectsubordinate(criteria).getResult();
        return areaList;

    }

    @GetMapping("/company")
    @ResponseBody
    public List<Department> company(@RequestParam Long id) {
        Map criteria = new HashMap();
        criteria.put("governmentId", id);
        List<Department> departments = departmentReadService.findList(criteria).getResult();
        return departments;
    }

    @GetMapping("/department")
    @ResponseBody
    public List<String> department(@RequestParam String governmentName) {
        Map criteria = new HashMap();
        criteria.put("governmentName", governmentName);
        List<String> departments = departmentReadService.selectByOrgan(criteria).getResult();
        return departments;
    }

    @PostMapping("/password")
    @ResponseBody
    public Boolean password(@RequestParam Long id,
                      /*      @RequestParam String oldpassword,*/
                            @RequestParam String password) {
        User user = (User) userReadService.findById(id).getResult();
        /*if (!MD5Util.match(oldpassword, user.getPassword())) {
            return false;
        }*/
        if (!Strings.isNullOrEmpty(password)) {
            user.setPassword(MD5Util.encrypt(password));
            return (Boolean) userWriteService.update(user).getResult();
        }
        return false;
    }

    @GetMapping("/editPassword")
    public String editPassword(@RequestParam Long id, Model model) {
        model.addAttribute("id", id);
        return "management/userManagement/userPassword";
    }


    public Map getarea(HttpServletResponse response, HttpServletRequest request) {
        List<String> list = new ArrayList<>();
        list.add("province");
        list.add("city");
        list.add("region");
        list.add("street");
        list.add("village");
        Cookie[] cookies = null;
        cookies = request.getCookies();
        String name = "";
        Map map = new HashMap();
        for (Cookie cookie : cookies) {

            if (cookie.getName().equals("djtid")) {
                String id = cookie.getValue();
                int Id = Integer.parseInt(id);
                Response<User> userResponse = userReadService.findById(Integer.toUnsignedLong(Id));
                name = userResponse.getResult().getName();
                map.put("username", name);
                OrganizationUser organizationUser = organizationUserReadService.findOrganizationUserByUserId(userResponse.getResult().getId()).getResult();
                switch (userResponse.getResult().getAreaLevel()) {
                    case 1:

                        UserProfile userProfile = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String Province = userProfile.getProvince();


                        //获取所有省下城市

                        Map map1 = new HashMap();
                        map1.put("area1", list.get(1));
                        map1.put("area2", list.get(0));
                        map1.put("area3", Province);
                        List<String> citys = breedFactoryReadService.serachAllregion1(map1);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList = new ArrayList<>();
                        for (String city : citys) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(2));
                            map2.put("area2", list.get(1));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList.add(cityAreaDto);

                        }
                        if (organizationUser.getOrganizationType() == 3) {
                            Map map6 = new HashMap();
                            map6.put("province", Province);
                            List<BreedFactory> breedFactoryList = (List<BreedFactory>) breedFactoryReadService.findByArea(map6).getResult();
                            map.put("breedFactorys", breedFactoryList);
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        } else {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        }

                        map.put("cityAreaDtoList", cityAreaDtoList);


                        //switch break;
                        break;
                    case 2:
                        UserProfile userProfile2 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String city1 = userProfile2.getCity();

                        //获取所有省下城市

                        Map map21 = new HashMap();
                        map21.put("area1", list.get(2));
                        map21.put("area2", list.get(1));
                        map21.put("area3", city1);
                        List<String> citys2 = breedFactoryReadService.serachAllregion1(map21);
                        //获取城市下所有区
                        List<CityAreaDto> cityAreaDtoList1 = new ArrayList<>();
                        for (String city : citys2) {
                            CityAreaDto cityAreaDto = new CityAreaDto();
                            Map map2 = new HashMap();
                            map2.put("area1", list.get(3));
                            map2.put("area2", list.get(2));
                            map2.put("area3", city);
                            List<String> regions = breedFactoryReadService.serachAllregion1(map2);
                            cityAreaDto.setCity(city);
                            cityAreaDto.setRegions(regions);
                            cityAreaDtoList1.add(cityAreaDto);

                        }
                        if (organizationUser.getOrganizationType() == 3) {
                            Map map7 = new HashMap();
                            map7.put("city", city1);
                            List<BreedFactory> breedFactoryList1 = (List<BreedFactory>) breedFactoryReadService.findByArea(map7).getResult();
                            map.put("breedFactorys", breedFactoryList1);

                        } else {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        }
                        map.put("cityAreaDtoList", cityAreaDtoList1);

                        //switch break;
                        break;
                    case 3://区
                        UserProfile userProfile3 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String region = userProfile3.getRegion();

                        List<CityAreaDto> cityAreaDtoList3 = new ArrayList<>();
                        CityAreaDto cityAreaDto = new CityAreaDto();
                        List<String> regions = new ArrayList<>();
                        regions.add(region);
                        cityAreaDto.setCity(userProfile3.getCity());
                        cityAreaDto.setRegions(regions);
                        cityAreaDtoList3.add(cityAreaDto);


                        if (organizationUser.getOrganizationType() == 3) {
                            Map map36 = new HashMap();
                            map36.put("region", region);
                            List<BreedFactory> breedFactoryList3 = (List<BreedFactory>) breedFactoryReadService.findByArea(map36).getResult();
                            map.put("breedFactorys", breedFactoryList3);
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        } else {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        }
                        map.put("cityAreaDtoList", cityAreaDtoList3);


                        //switch break;
                        break;
                    case 4://镇
                        UserProfile userProfile4 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();
                        String street = userProfile4.getStreet();

                        //获取所有镇下所有村

                        Map map41 = new HashMap();
                        map41.put("area1", list.get(4));
                        map41.put("area2", list.get(3));
                        map41.put("area3", street);
                        List<String> citys4 = new ArrayList<>();//所有村
                        citys4.add(userProfile4.getRegion());
                        List<CityAreaDto> cityAreaDtoList4 = new ArrayList<>();

                        CityAreaDto cityAreaDto3 = new CityAreaDto();

                        cityAreaDto3.setCity(userProfile4.getCity());
                        cityAreaDto3.setRegions(citys4);
                        cityAreaDtoList4.add(cityAreaDto3);
                        if (organizationUser.getOrganizationType() == 3) {
                            Map map46 = new HashMap();
                            map46.put("street", street);
                            List<BreedFactory> breedFactoryList4 = (List<BreedFactory>) breedFactoryReadService.findByArea(map46).getResult();
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                            map.put("breedFactorys", breedFactoryList4);
                        } else {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        }


                        map.put("cityAreaDtoList", cityAreaDtoList4);

                        //switch break;
                        break;
                    case 5://村
                        UserProfile userProfile5 = userProfileReadService.findUserProfileById(userResponse.getResult().getId()).getResult();

                        List<GovernmentUserDuty> governmentUserDutyList = governmentUserDutyReadService.findGovernmentUserDutiesByUserId(userResponse.getResult().getId(), null).getResult();
                        List<Long> longlist = new ArrayList<>();
                        if (governmentUserDutyList.size() != 0) {
                            for (GovernmentUserDuty governmentUserDuty : governmentUserDutyList) {
                                longlist.add(governmentUserDuty.getFactoryOrPointId());
                            }
                        }
                        List<String> villages = new ArrayList<>();
                        List<BreedFactory> breedFactoryList2 = breedFactoryReadService.findBreedFactoryDtoByIds(longlist).getResult();
                        for (BreedFactory breedFactory : breedFactoryList2) {
                            villages.add(breedFactory.getVillage());
                            ;
                        }

                        List<String> listWithoutDup = new ArrayList<String>();
                        List<CityAreaDto> cityAreaDtoList5 = new ArrayList<>();
                        CityAreaDto cityAreaDto1 = new CityAreaDto();
                        cityAreaDto1.setCity(userProfile5.getCity());
                        listWithoutDup.add(userProfile5.getRegion());

                        // cityAreaDto1.setCity(village);
                        cityAreaDto1.setRegions(listWithoutDup);

                        cityAreaDtoList5.add(cityAreaDto1);

                        map.put("userProfile", userProfile5);
                        if (organizationUser.getOrganizationType() == 3) {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                            map.put("breedFactorys", breedFactoryList2);
                        } else {
                            map.put("OrganizationType", organizationUser.getOrganizationType());
                        }

                        map.put("cityAreaDtoList", cityAreaDtoList5);

                        //switch break;
                        //switch break;
                        break;
                }

                break;
            }

        }

        return map;
    }


}
